package com.local.localRestful.config;

import com.local.localRestful.domain.User;
import com.local.localRestful.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * Created by zzy on 2018/1/14.
 */
@Configuration
public class RouterFunctionConfiguration {

    /**
     * Reterf:提高吞吐量，异步方式
     * Servler 请求接口：ServletRequest 或者 HttpRequest
     * Spring 5.0 重新定义了服务请求和相应接口
     * ServerRequest
     * ServerResponse
     * 支持Servlet规范，也支持自定义，比如Netty(Web Server)
     * 定义GET请求，返回素有的用户对象 URI:/person/find/all
     */
    @Bean
    @Autowired
    public RouterFunction<ServerResponse> personFindAll(UserRepository userRepository){

       return RouterFunctions.route(RequestPredicates.GET("/person/find/all"),
                request->{
                    //返回所有对象
                    List<User> users=userRepository.findAll();
                    Flux<User> userFlux=Flux.fromIterable(users);
                   return ServerResponse.ok().body(userFlux,User.class);
                });
    }
}
